package no.ntnu.fp.gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Color;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import no.ntnu.fp.client.ClientProgramStart;
import no.ntnu.fp.model.Person;
import no.ntnu.fp.server.DBConnection;

import javax.swing.UIManager;
import java.awt.CardLayout;
import javax.swing.JPasswordField;
import javax.swing.JLayeredPane;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class LoginWindow extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1497784519419953443L;
	private JPanel contentPane;
	private JTextField userNametextField;
	private JPasswordField passwordField;
	private JLabel lblFeil;
	
	/**
	 * Launch the application.
	 */
	public static void start() {
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginWindow frame = new LoginWindow();
					frame.setLocationRelativeTo(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	

	/**
	 * Create the frame.
	 */
	public LoginWindow() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 536, 327);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(5, 5, 508, 275);
		panel.setBackground(Color.WHITE);
		contentPane.add(panel);
		panel.setLayout(null);
	
		panel.setBackground(Color.WHITE);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblCalender = new JLabel("<html><pre>Calendar 2011<br></br> - Corporate Edition</pre></html>\r\n\t\t");
		lblCalender.setForeground(UIManager.getColor("ProgressBar.selectionBackground"));
		lblCalender.setBounds(151, -2, 249, 94);
		lblCalender.setFont(new Font("Trebuchet MS", Font.BOLD, 20));
		panel.add(lblCalender);
		
		JLabel lblBrukernavn = new JLabel("Brukernavn:");
		lblBrukernavn.setBounds(7, 108, 125, 14);
		panel.add(lblBrukernavn);
		
		userNametextField = new JTextField();
		this.setFocusable(true);
		this.addKeyListener(new KeyAdapter() {
			
			@Override
			public void keyPressed(KeyEvent arg0) {
				
				if(arg0.getKeyCode() == KeyEvent.VK_ENTER){
					enterPressed();
				}
			}
			});
		
		userNametextField.addKeyListener(new KeyAdapter() {
			
			@Override
			public void keyPressed(KeyEvent arg0) {
				
				if(arg0.getKeyCode() == KeyEvent.VK_ENTER){
					enterPressed();
				}
			}
			});
		
		userNametextField.setBounds(142, 105, 258, 20);
		panel.add(userNametextField);
		userNametextField.setColumns(10);
		
		JLabel lblPassord = new JLabel("Passord:");
		lblPassord.setBounds(7, 139, 125, 14);
		panel.add(lblPassord);
		
		JCheckBox chckbxRememberMe = new JCheckBox("Husk meg");
		chckbxRememberMe.setBackground(Color.WHITE);
		chckbxRememberMe.setBounds(142, 199, 104, 23);
		panel.add(chckbxRememberMe);
		
		JButton btnLoggInn = new JButton("Logg inn");
		btnLoggInn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//sjekk om brukernavn og passord passer.
				
				DBConnection dbc = new DBConnection();
				String user = userNametextField.getText();
				
				String myPassword = dbc.selectPassword(user);
				
				char[] typedPassword = passwordField.getPassword();
				String a = "";
				for(int i = 0; i<typedPassword.length; i++){
					a += typedPassword[i];
				}
			
				
				if(myPassword == null){
					lblFeil.setVisible(true);
					passwordField.setText("");
					userNametextField.setText("");
				}
			
				else if(myPassword.equals(a)){
					Person p = dbc.selectPersonByUsername(user);
					ClientProgramStart.cps.loginOk(p);
					processWindowEvent(new WindowEvent(LoginWindow.this, WindowEvent.WINDOW_CLOSING));
				}
				else{
					lblFeil.setVisible(true);
					passwordField.setText("");
					userNametextField.setText("");
				}
			}
		});
		btnLoggInn.setBounds(289, 199, 111, 23);
		panel.add(btnLoggInn);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(142, 138, 258, 20);
		panel.add(passwordField);
		
		passwordField.addKeyListener(new KeyAdapter() {
			
			@Override
			public void keyPressed(KeyEvent arg0) {
				
				if(arg0.getKeyCode() == KeyEvent.VK_ENTER){
					enterPressed();
				}
			}
		});
		
		lblFeil = new JLabel("Feil brukernavn eller passord");
		lblFeil.setForeground(Color.RED);
		lblFeil.setBounds(142, 169, 195, 14);
		panel.add(lblFeil);
		lblFeil.setVisible(false);
		
	}
	
	
	public void enterPressed(){
		
		DBConnection dbc = new DBConnection();
		String user = userNametextField.getText();
		
		String myPassword = dbc.selectPassword(user);
		
		char[] typedPassword = passwordField.getPassword();
		String a = "";
		for(int i = 0; i<typedPassword.length; i++){
			a += typedPassword[i];
		}
		
		if(myPassword == null){
			lblFeil.setVisible(true);
			passwordField.setText("");
			userNametextField.setText("");
		}
	
		else if(myPassword.equals(a)){
			Person p = dbc.selectPersonByUsername(user);
			ClientProgramStart.cps.loginOk(p);
			processWindowEvent(new WindowEvent(LoginWindow.this, WindowEvent.WINDOW_CLOSING));
		}
		else{
			lblFeil.setVisible(true);
			passwordField.setText("");
			userNametextField.setText("");
		}
	}
}
